﻿using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Web.UI;
using JQuery.WebKit.Base;

namespace JQuery.WebKit
{
    [ParseChildren(typeof(JValidationMessage), DefaultProperty = "Messages", ChildrenAsProperties = true)]
    public class JValidationSummary : WebControlBase
    {
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public JValidationMessageCollection Messages { get; private set; }

        public JValidationSummary()
        {
            Messages = new JValidationMessageCollection();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Messages.ForEach(item => base.Controls.Add(item));
        }

        public void DisplayErrors(ArrayList errorList)
        {
            Messages.Clear();

            foreach (object errorObj in errorList)
            {
                if (errorObj as Exception != null)
                {
                    if ((errorObj as Exception).Message.StartsWith("The DELETE statement conflicted with the REFERENCE constraint"))
                    {
                        Messages.Add(JQuery.WebKit.Messages.ReferenceError, JValidationType.Error);
                    }
                    else if ((errorObj as Exception).Message.ToLower().StartsWith("duplicate key"))
                    {
                        Messages.Add(JQuery.WebKit.Messages.DuplicateKey, JValidationType.Error);
                    }
                    else
                    {
                        Messages.Add((errorObj as Exception).Message, JValidationType.Error);
                    }
                }
            }
            EnsureChildControls();
        }

        protected override void EnsureChildControls()
        {
            if (this.Controls.Count == 0)
            {
                CreateChildControls();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            EnsureChildControls();
            base.OnPreRender(e);
        }

        public virtual string InnerHtml
        {
            get { return GetInnerHtml(); }
        }

        private string GetInnerHtml()
        {
            EnsureChildControls();
            using (StringWriter writer = new StringWriter())
            {
                using (HtmlTextWriter writer2 = new HtmlTextWriter(writer))
                {
                    this.RenderContents(writer2);
                    return writer.ToString();
                }
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            for (int i = 0; i < Messages.Count; i++)
            { 
                Messages[i].RenderControl(writer);
                if (i + 1 < Messages.Count)
                { 
                    writer.Write(new LiteralControl("<br />"));
                }
            }
        }
    }
}
